<template>
  <div class="gamePackage">
    <top-bar :title="'游戏礼包'"/>
    <div class="scroll">
      <!-- 充值礼包 -->
      <div class="recharge">
        <div class="rechargeBg drawStyleBg" v-if="rechargeList.length != 0">
          充值礼包
        </div>
        <div class="recharge-content" v-if="giftList">
          <div class="recharge-items" v-for="item in rechargeList" :key="item.gift_id">
            <div class="recharge-items-left" @click="$router.push(`/me/packageDetails/${item.gift_id}`)">
              <div>
                <img src="@/assets/images/personal_center/gifts.png" alt="">
                <span>{{ item.title }}</span>
              </div>
              <span>{{ item.content }}</span>
            </div>
            <div v-if="item.gift_code" v-clipboard:copy="item.gift_code"
                 v-clipboard:success="onCopy"
                 v-clipboard:error="onError" class="recharge-items-right" :class="'use'">
              复制
            </div>
            <div v-else @click="addGift(item)" class="recharge-items-right"
                 :class="[item.remain_cnt==0 ? 'done' : 'draw']">
              {{ item.remain_cnt == 0 ? '已抢完' : '领取' }}
            </div>
          </div>
        </div>
      </div>
      <!-- 免费礼包 -->
      <div class="free">
        <div class="freeBg drawStyleBg" v-if="freeList.length != 0">
          免费礼包
        </div>
        <div class="recharge-content" v-if="giftList">
          <div class="recharge-items" v-for="item in freeList" :key="item.gift_id">
            <div class="recharge-items-left" @click="$router.push(`/me/packageDetails/${item.gift_id}`)">
              <div>
                <img src="@/assets/images/personal_center/gifts.png" alt="">
                <span>{{ item.title }}</span>
              </div>
              <span>{{ item.content }}</span>
            </div>
            <div v-if="item.gift_code" v-clipboard:copy="item.gift_code"
                 v-clipboard:success="onCopy"
                 v-clipboard:error="onError" class="recharge-items-right" :class="'use'">
              复制
            </div>
            <div v-else @click="addGift(item)" class="recharge-items-right"
                 :class="[item.remain_cnt==0 ? 'done' : (item.gift_code=='' ? 'draw' : 'use')]">
              {{ item.remain_cnt == 0 ? '已抢完' : '领取' }}
            </div>
          </div>
        </div>
      </div>
      <div v-if="giftList.count == 0" class="list-empty">
        <i class="iconfont icon-kong"></i>
        <p>暂时没有礼包哦~</p>
      </div>
    </div>
    <el-dialog
      :modal-append-to-body='false'
      :visible.sync="dialogVisible"
      :showClose="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="giftDetail"
    >
      <h1>领取成功</h1>
      <div class="giftBox">
        <span>礼包码</span>
        <div>
          <span>{{ giftDetail.gift_code }}</span>
          <button v-clipboard:copy="giftDetail.gift_code"
                  v-clipboard:success="onCopy"
                  v-clipboard:error="onError">复制
          </button>
        </div>
      </div>
      <div class="usageMethod">
        {{ usageMethod }}
      </div>
      <div class="buttons">
        <button @click="know" class="know">我知道了</button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import topBar from './topBar'
import * as api from '../../api'

export default {
  name: 'gamePackage',
  components: {
    topBar
  },
  data () {
    return {
      dialogVisible: false,
      freeList: [],
      rechargeList: [],
      usageMethod: '',
      giftDetail: null,
    }
  },
  computed: {
    ...mapGetters([
      'failMsg'
    ]),
    giftList () {
      this.rechargeList = []
      this.freeList = []
      for (let i of this.$store.state.giftList.list) {
        if (i.gift_type == 4) {
          this.rechargeList.push(i)
        } else if (i.gift_type == 1) {
          this.freeList.push(i)
        }
      }
      return this.$store.state.giftList
    }
  },
  methods: {
    // 领取/使用
    addGift (item) {
      if (item.total_cnt !== 0) {
        api.addGift(item.gift_id, succ => {
          api.getGiftDetail(item.gift_id, succ => {
            this.giftDetail = succ
            if (this.giftDetail.func != undefined) {
              this.usageMethod = this.giftDetail.func.replace(/\r\n/g, '<br>')
            }
            this.dialogVisible = true
          })
        }, error => {
          this.Toast(error, 500)
        })
      }
    },
    // 点击跳游戏礼包详情页
    skipDetails (id) {
      this.$router.push({path: '/me/packageDetails', params: {id: id}})
    },
    // 礼包码复制
    onCopy () {
      this.Toast('复制成功', 500)
    },
    onError () {
      this.Toast('复制失败', 500)
    },
    // 关闭礼包领取成功弹窗
    know () {
      this.dialogVisible = false
      this.$store.dispatch('getGiftList')
    }
  },
  watch: {
    failMsg () {
      this.failMsg && this.Toast(this.failMsg, 500)
    }
  },
  created () {
    this.$store.dispatch('getGiftList')
  }
}
</script>

<style lang="scss" scoped>

.gamePackage {
  padding: 0.75rem 0;
  position: relative;

  /deep/ .title {
    padding: 0 0.48rem;
    margin-bottom: 0.48rem;
  }

  .popContainer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
  }

  .scroll {
    height: 8.5rem;
    overflow: scroll;

    .drawStyleBg {
      margin-top: 0.48rem;
      width: 2rem;
      height: 0.75rem;
      font-size: 0.37rem;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 0.75rem;
      text-align: center;
      letter-spacing: 0.02rem;
    }

    .recharge, .free {
      .rechargeBg {
        margin-top: 0;
        background: url("../../assets/images/personal_center/recharge.png") no-repeat;
        background-size: 100% 100%;
      }

      .recharge-content {
        margin-top: 0.16rem;
        padding: 0 0.48rem;

        .recharge-items {
          width: 100%;
          height: 2rem;
          background: rgba(255, 255, 255, 0.6);
          border-radius: 0.27rem;
          padding: 0.5rem 0.37rem 0.5rem 0.45rem;
          align-items: center;
          display: flex;
          margin-bottom: 0.32rem;

          .recharge-items-left {
            display: flex;
            flex-direction: column;
            flex: 1;

            > div {
              display: flex;
              align-items: center;

              img {
                width: 0.45rem;
                height: 0.45rem;
                margin-right: 0.2rem;
              }

              span {
                font-size: 0.35rem;
                font-weight: bold;
                color: #181818;
                margin-top: 0.08rem;
              }
            }

            > span {
              margin-top: 0.24rem;
              font-size: 0.27rem;
              font-weight: 400;
              color: #888888;
              width: 6.4rem;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }

          .recharge-items-right {
            width: 1.49rem;
            height: 0.64rem;
            border-radius: 0.33rem;
            font-size: 0.32rem;
            font-weight: 400;
            text-align: center;
            line-height: 0.64rem;
          }

          .use {
            background: #FFD200;
            color: #000000;
          }

          .draw {
            background: #FF0000;
            color: #FFFFFF;
          }

          .done {
            background: #F5F5F5;
            color: #B5B5B5;
          }
        }
      }
    }

    .freeBg {
      margin-top: 0.37rem;
      background: url("../../assets/images/personal_center/free.png") no-repeat;
      background-size: 100% 100%;
    }
  }

  /deep/ .el-dialog {
    width: 9.2rem !important;
    height: 8.64rem;
    background: #FFFFFF;
    border-radius: 0.21rem;
    margin: 0;
    margin-left: 0.4rem;
    margin-bottom: 0.91rem;

    h1 {
      font-size: 0.43rem;
      font-weight: bold;
      color: #292934;
      text-align: center;
      margin-bottom: 0.48rem;
    }

    .giftBox {
      display: flex;
      align-items: center;

      > span {
        margin-right: 0.21rem;
        font-size: 0.37rem;
        font-weight: 500;
        color: #363739;
      }

      > div {
        width: 6.99rem;
        height: 1.39rem;
        background: #FFF9F9;
        border-radius: 0.21rem;
        display: flex;
        align-items: center;

        > span {
          font-size: 0.35rem;
          font-weight: bold;
          color: #FF0300;
          overflow: hidden;
          flex: 1;
          margin-left: 0.53rem;
          height: 0.5rem;
        }

        > button {
          width: 1.17rem;
          height: 0.43rem;
          border-radius: 0.33rem;
          border: 0.02rem solid #999999;
          font-size: 0.35rem;
          font-weight: 400;
          color: #999999;
          background: none;
          margin-right: 0.29rem;
        }
      }
    }

    .usageMethod::-webkit-scrollbar {
      width: 0 !important
    }

    .usageMethod {
      height: 3.04rem;
      background: #FFF9F9;
      border-radius: 0.21rem;
      font-size: 0.35rem;
      font-weight: 400;
      color: #999999;
      padding: 0.45rem 0.35rem;
      word-wrap: break-word;
      word-break: break-all;
      overflow: scroll;
      margin-top: 0.32rem;
    }

    .buttons {
      text-align: center;
      margin-top: 0.67rem;

      .know {
        width: 8.13rem;
        height: 1.2rem;
        background: #FF751A linear-gradient(90deg, #FF0000 0%, #FFD500 100%);
        border-radius: 0.67rem;
        font-size: 0.45rem;
        font-weight: 500;
        color: #FFFFFF;
      }
    }
  }

  /deep/ .el-dialog__header {
    padding: 0;
  }

  /deep/ .el-dialog__body {
    padding: 0.51rem 0.4rem;
  }

  /deep/ .el-dialog__wrapper {
    top: auto;
  }
}
</style>
